<?php

namespace App\Http\Controllers;

use App\Exceptions\ValidateException;
use App\Model\Goods;
use App\Repository\Contracts\TaoCangInterface;
//use App\Repository\Contracts\TaoCangjavaInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use App\Repository\Contracts\ShoppingCartInterface;
use App\Repository\Contracts\GoodsInterface;
use Auth;
use Illuminate\Support\Facades\DB;



class TaoCangController extends Controller
{

    /**
     * @var Request
     */
    protected $request;

    /**
     * @var TaoCangInterface
     */
    protected $taocang;


    protected $uid;

    /**
     * TaoCangController constructor.
     * @param Request $request
     * @param TaoCangInterface $taoCang
     * @param TaoCangjavaInterface $taoCangJava
     */
    public function __construct(Request $request, TaoCangInterface $taoCang,ShoppingCartInterface $shoppingCart,GoodsInterface $goods)
    {
        $this->request = $request;

        $this->taocang = $taoCang;

        $this->shopping=$shoppingCart;

        $this->goods=$goods;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $admin_id=$this->request->cookie('admin_id');
        $searchMap = $this->searchMap([
            ['admin_id', $admin_id, '='],
        ]);
        $list = $this->taocang->lists($limit, $page,$searchMap);
        return $this->success($list);
    }
    public function goodsList(Request $request){
        $goodsName = $request->input('goods_name');
        $list = Goods::where('zh_name','like','%'.$goodsName.'%')->get();
        return $this->success($list);
    }

    public function findzr($id){
        $uid = $id;
        $map = $this->searchMap([
            ['uid', $uid, '='],
            ['admin_id', $this->uid, '='],
            ['status', 1, '=']
        ]);
        $list = $this->shopping->lists($map)->toArray();
        $zheran =0;
        $zong=0;
        foreach ($list as $row){
            $zong=$zong+$row['price']*$row['counts'];
            $goods=$this->goods->detail($row['goods_id'])->toArray();
            //判断有没有套餐
            if($goods['taocang_id'] > 0){
                $detail = $this->taocang->detail($goods['taocang_id'])->toArray();
                $goods_arr=$detail['goods_arr'];
                $goods_id_list=explode(',',$goods_arr);
                //判断套餐商品是否全在
                $tao=DB::table('shopping_cart')
                    ->whereIn('goods_id', $goods_id_list)
                    ->get()->toArray();

                //计算当前套餐商品总金额
                $amount=0;
                foreach ($goods_id_list as $value){
                    $map = $this->searchMap([
                        ['admin_id', $this->uid, '='],
                        ['status', 1, '='],
                        ['goods_id',$value,'=']
                    ]);
                    $list = $this->shopping->lists($map)->toArray();
                    if(!empty($list)){
                        $amount=$amount+$list[0]['price']*$list[0]['counts'];
                    }
                }
                if(count($goods_id_list) == count($tao) ){//如果全部在，我们就给他折扣
                    if($detail['type'] == 1){
                        if($detail['vector'] > 0){
                            if($detail['vector'] <= $amount){
                                $zheran+=$detail['discount']/count($goods_id_list);
                            }
                        }else{
                            $zheran+=$detail['discount']/count($goods_id_list);
                        }
                    }else{
                        if($detail['vector'] > 0){
                            if($detail['vector'] <= $amount){
                                $zheran+=(($amount*$detail['discount'])/100);
                            }
                        }else{
                            $zheran+=(($amount*$detail['discount'])/100);
                        }
                    }
                }
            }
        }
        $data=array(
            'zong'=> round($zong,2),
            'zhe'=>round($zheran,2)
        );
        return $this->success($data);
    }
    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $detail = $this->taocang->detail($id);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data = $this->request->only(['title', 'status','type','vector','discount','goods_arr']);
            $data['goods_arr']=implode(",", $data['goods_arr']);
            $admin_id=$this->request->cookie('admin_id');
            $data['admin_id']=$admin_id;
            $id = $this->taocang->created($data);
            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * Update
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updated($id)
    {
        try {
            $data = $this->request->only(['name', 'status']);
            $result = $this->taocang->updated($id, $data);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function initCategory()
    {
        if (!env('DIRECT_SALE') || !env('JAVA_NETWORK')) {
            return $this->success();
        }

        try {
            $detail = $this->goodsCategory->findIdForFirst();
//            $result = $this->goodsCategoryJava->syncList(isset($detail[0])?$detail[0]['created_at']:0);

//            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

}